/*
 * bwOsd.cpp
 *
 *  Created on: 2018年11月9日
 *      Author: linsainan
 */

#include "bw_Osd.h"
#include "bw_FcgiRequest.h"
#include "bw_dbcol.h"
#include "bw_Sqlite.h"
#include "cJSON.h"
#include "bw_jskey.h"
#include "bw_DateTime.h"
#include "bw_Base64.h"
#include "bw_public.h"
#include "bw_Str.h"
#include "bw_Sys.h"

using namespace bwcgi;

extern char g_outbuf[];
extern int jsonMsgtoStr(cJSON *pout_root,char *gbuf,unsigned int maxsize);

// logo生成/编辑
const char* logo_edit(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;

	int chl_id = (s_id - 1) * c_id;

//*****************************************//
	char cmd_buf[256]={0};
	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);
	
	char sql[SZ_1K] = { 0 };
	
	p_tmp = cJSON_GetObjectItem(param, "slide_interval");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_OSD_SLIDE_INTERVAL_MAX) 
	{
		cgi_debug("slide_interval error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int slide_interval = p_tmp->valueint;

	p_tmp = cJSON_GetObjectItem(param, "opacity");//空间透明度
	if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint > 128) 
	{
		cgi_debug("opacity error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int opacity = p_tmp->valueint;

	p_tmp = cJSON_GetObjectItem(param, "level");
	if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint >= ENC_OSD_MAX_OVERLAY) 
	{
		cgi_debug("level error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int level = p_tmp->valueint;

	// width
	p_tmp = cJSON_GetObjectItem(param, "width");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("width error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int width = p_tmp->valueint;

	// height
	p_tmp = cJSON_GetObjectItem(param, "height");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("height error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int height = p_tmp->valueint;

	// p_x
	p_tmp = cJSON_GetObjectItem(param, "p_x");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("p_x error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int p_x = p_tmp->valueint;

	// p_y
	p_tmp = cJSON_GetObjectItem(param, "p_y");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("p_y error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int p_y = p_tmp->valueint;

	// img_url：logo 最多可以保存3张
	cJSON * img_root = cJSON_GetObjectItem(param, "img_url");
	if (!img_root) 
	{
		cgi_debug("img_url error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	cJSON *img_item = NULL;
	unsigned int img_num = cJSON_GetArraySize(img_root);
	if (img_num <= 0 || img_num > 3) 
	{
		cgi_debug("img_num error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// period
	p_tmp = cJSON_GetObjectItem(param, "period");
	if (!p_tmp) 
	{
		cgi_debug("no period error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	cJSON *pe_item = NULL;
	unsigned int pe_num = cJSON_GetArraySize(p_tmp);
	if (pe_num <= 0) 
	{
		cgi_debug("parse period array num error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	int ret = db.Exec("begin;");	// 开启事务
	if (SQLITE_OK != ret) 
	{
		cgi_debug("start transaction error");
		cJSON_Delete(root);
		return bwenc_common_ack(BWENC_ERROR_DB_ERR);
	}

	char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
	DateTime dt;
	dt.time2string(time(nullptr), timeStr);
	

	// 插入period
	cJSON* period_tmp = NULL;
	for (unsigned int i = 0; i < pe_num; i++) 
	{
		pe_item = cJSON_GetArrayItem(p_tmp, i);
		period_tmp = cJSON_GetObjectItem(pe_item, "day");
		if (!period_tmp || period_tmp->valueint < 0 || period_tmp->valueint > 6) 
		{
			cgi_debug("period day error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		int day = period_tmp->valueint;

		// start_time
		period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
		DateTime dt;
			
		if (!period_tmp || !period_tmp->valuestring || strlen(period_tmp->valuestring) != 8 || !dt.timeCheck(period_tmp->valuestring)) 
		{
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
				
		char start_time[10] = { '\0' };
		memcpy(start_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// end_time
		period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
		if (!period_tmp || !period_tmp->valuestring || strlen(period_tmp->valuestring) != 8 || !dt.timeCheck(period_tmp->valuestring)) 
		{
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		char end_time[10] = { 0 };
		memcpy(end_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// state
		int state = 0;
		period_tmp = cJSON_GetObjectItem(pe_item, "state");
		if (!period_tmp
				|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
			cgi_debug("period state error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		state = period_tmp->valueint;

		//先删除原来的
		snprintf(sql, sizeof(sql),
				"delete from %s where %s=%d and %s=%d",
				DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_LEVEL,level);
		if (SQLITE_OK != db.Exec(sql)) 
		{
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		
		// 插入period数据
		snprintf(sql, sizeof(sql),
				"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
				DBTBL_OSD_PERIOD, 
				DBCOL_CHL_ID, DBCOL_LEVEL, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
				chl_id, level, day, start_time, end_time,state);
		if (SQLITE_OK != db.Exec(sql)) 
		{
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
	}


	//设置到共享内存；
	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;
	stENC_LOGOSET* logo_info = NULL;
	
	if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
	{
		chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
		osd_info = &(chl_info->encchl_osd[level]);

		//清空
		memset(osd_info,0,sizeof(stENC_OSDSET));
		
		osd_info->osd_enable = 1;
		osd_info->osd_type = BWENC_OSD_TYPE_LOGO;

		logo_info = &(osd_info->osd_cfg.osd_logo);
		logo_info->logo_opacity = opacity;
		logo_info->logo_width = width;
		logo_info->logo_height = height;
		logo_info->logo_x = p_x;
		logo_info->logo_y = p_y;
		logo_info->logo_movspeed = slide_interval;
		logo_info->logo_num = img_num;

		for (unsigned int j = 0; j < img_num; j++) 
		{
			if( j >= ENC_OSD_MAX_SLIDE_NUM)
			{
				break;
			}
			
			img_item = cJSON_GetArrayItem(img_root, j);
			if (img_item) 
			{
				snprintf((char *)logo_info->logo_url[j], sizeof(logo_info->logo_url[j]), "%s",img_item->valuestring);
			}

		}

		//生效时间
		for(int d = 0; d < ENC_WEEK_DAYS; d++)
		{
			snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
										DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_LEVEL,level,DBCOL_DAY, d,DBCOL_STATE);

			BWRows period_rows = db.QeuryRows(sql);
			if(period_rows.empty() || period_rows.size()==0)
			{
				continue;
			}
			
			for (unsigned int t = 0; t < period_rows.size(); t++) 
			{
				if(t >= ENC_OSD_MAX_TM_NUM)
				{
					break;
				}
				
				BWRow p_row = period_rows[t];

				(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
				(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
			}
		}

		chl_info->encchl_setvsion++;
	}

	snprintf(logstr,sizeof(logstr),"slot:%d-ch:%d Logo set successfully",s_id,c_id);
	log_write(BWENC_LOG_SYS_SET, logstr);

	cJSON_Delete(root);
	return bwenc_common_ack(code);
}

// 字幕生成/编辑
const char* sub_edit(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;

	int chl_id = (s_id - 1) * c_id;

	char cmd_buf[256]={0};
	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);
	
	char sql[SZ_1K] = { 0 };

	p_tmp = cJSON_GetObjectItem(param, "vd_format");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0) {
		cgi_debug("vd_format error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int vd_format = std::stoi(p_tmp->valuestring);
	if (vd_format < BWENC_VD_1920 || vd_format > BWENC_VD_480) {
		cgi_debug("vd_format value error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	p_tmp = cJSON_GetObjectItem(param, "roll_speed");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| (strcmp(p_tmp->valuestring, BWENC_RS_SLOW) != 0
					&& strcmp(p_tmp->valuestring, BWENC_RS_NORMAL) != 0
					&& strcmp(p_tmp->valuestring, BWENC_RS_QUICK) != 0)) {
		cgi_debug("roll_speed error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char roll_speed[10] = { 0 };
	memcpy(roll_speed, p_tmp->valuestring, strlen(p_tmp->valuestring));

	p_tmp = cJSON_GetObjectItem(param, "level");
	if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint >= ENC_OSD_MAX_OVERLAY) {
		cgi_debug("level error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int level = p_tmp->valueint;

	// dis_w
	p_tmp = cJSON_GetObjectItem(param, "dis_w");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("dis_w error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int dis_w = p_tmp->valueint;

	// dis_h
	p_tmp = cJSON_GetObjectItem(param, "dis_h");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("dis_h error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int dis_h = p_tmp->valueint;

	// p_x
	p_tmp = cJSON_GetObjectItem(param, "p_x");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("p_x error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int p_x = p_tmp->valueint;

	// p_y
	p_tmp = cJSON_GetObjectItem(param, "p_y");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("p_y error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int p_y = p_tmp->valueint;

	// font
	p_tmp = cJSON_GetObjectItem(param, "font");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| (strcmp(p_tmp->valuestring, BWENC_FONT_AILERON) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_IDEALIST) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_KELSON) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_BITTER) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_ZWODERI) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_BRAXTON) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_SIMHEI) != 0) )
	{
		cgi_debug("font error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char font[20] = { 0 };
	memcpy(font, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// font_size
	p_tmp = cJSON_GetObjectItem(param, "font_size");
	//if (!p_tmp || p_tmp->valueint < BWENC_FONT_SIZE_MIN || p_tmp->valueint > BWENC_FONT_SIZE_MAX)
	if (!p_tmp) 
	{
		cgi_debug("font_size error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int font_size = p_tmp->valueint;

	// font_type
	p_tmp = cJSON_GetObjectItem(param, "font_type");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| (strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_NORMAL) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_BOLD) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_ITALIC) != 0)) {
		cgi_debug("font_type error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char font_type[10] = { 0 };
	memcpy(font_type, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// font_color
	p_tmp = cJSON_GetObjectItem(param, "font_color");

	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| strlen(p_tmp->valuestring) > 7) {
		cgi_debug("font_color error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	char font_color[7] = { 0 };
	memcpy(font_color, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// back_color
	p_tmp = cJSON_GetObjectItem(param, "back_color");

	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| strlen(p_tmp->valuestring) > 7) {
		cgi_debug("back_color error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	char back_color[7] = { 0 };
	memcpy(back_color, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// content
	p_tmp = cJSON_GetObjectItem(param, "content");
	if (!p_tmp || !p_tmp->valuestring
			|| strlen(p_tmp->valuestring)
					<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
		cgi_debug("content error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char content[BWENC_SUB_LEN_MAX] = { 0 };
	memcpy(content, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// period
	p_tmp = cJSON_GetObjectItem(param, "period");
	if (!p_tmp) {
		cgi_debug("no period error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	cJSON *pe_item = NULL;
	unsigned int pe_num = cJSON_GetArraySize(p_tmp);
	if (pe_num <= 0) {
		cgi_debug("parse period array num error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	int ret = db.Exec("begin;");	// 开启事务
	if (SQLITE_OK != ret) {
		cgi_debug("start transaction error");
		cJSON_Delete(root);
		return bwenc_common_ack(BWENC_ERROR_DB_ERR);
	}
	char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
	DateTime dt;
	dt.time2string(time(nullptr), timeStr);

	cJSON* period_tmp = NULL;
	for (unsigned int i = 0; i < pe_num; i++) 
	{
		pe_item = cJSON_GetArrayItem(p_tmp, i);
		period_tmp = cJSON_GetObjectItem(pe_item, "day");
		if (!period_tmp || period_tmp->valueint < 0
				|| period_tmp->valueint > 6) {
			cgi_debug("period day error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		int day = period_tmp->valueint;

		// start_time
		period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
		DateTime dt;
		if (!period_tmp || !period_tmp->valuestring
				|| strlen(period_tmp->valuestring) != 8
				|| !dt.timeCheck(period_tmp->valuestring)) {
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		char start_time[10] = { 0 };
		memcpy(start_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// end_time
		period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
		if (!period_tmp || !period_tmp->valuestring
				|| strlen(period_tmp->valuestring) != 8
				|| !dt.timeCheck(period_tmp->valuestring)) {
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		char end_time[10] = { 0 };
		memcpy(end_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// state
		int state = 0;
		period_tmp = cJSON_GetObjectItem(pe_item, "state");
		if (!period_tmp
				|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
			cgi_debug("period state error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		state = period_tmp->valueint;

		//先删除原来的
		snprintf(sql, sizeof(sql),
				"delete from %s where %s=%d and %s=%d",
				DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_LEVEL,level);
		if (SQLITE_OK != db.Exec(sql)) 
		{
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		
		// 插入period数据
		snprintf(sql, sizeof(sql),
				"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
				DBTBL_OSD_PERIOD, 
				DBCOL_CHL_ID, DBCOL_LEVEL, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
				chl_id, level, day, start_time, end_time,state);
		
		if (SQLITE_OK != db.Exec(sql)) {
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}

	}

	db.Exec("commit;");
	code = BWENC_ERROR_NONE;
	
//设置到共享内存
	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;
	stENC_SUBSET* sub_info = NULL;
		
	if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
	{
		chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
		osd_info = &(chl_info->encchl_osd[level]);

		//清空
		memset(osd_info,0,sizeof(stENC_OSDSET));
		
		osd_info->osd_enable = 1;
		osd_info->osd_type = BWENC_OSD_TYPE_TEXT;

		sub_info = &(osd_info->osd_cfg.osd_sub);
		
		snprintf((char *)sub_info->sub_content, sizeof(sub_info->sub_content),"%s",content);
		//snprintf((char *)sub_info->sub_font, sizeof(sub_info->sub_font),"%s",font);
		snprintf((char *)sub_info->sub_font, sizeof(sub_info->sub_font),"%s.%s",font,"ttf");
		
		sub_info->sub_fontsize = font_size;
		
		if (0 == strcmp(font_type,BWENC_FONT_AILERON)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_AILERON_T;
		} 
		else if (0 == strcmp(font_type,BWENC_FONT_IDEALIST)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_IDEALIST_T;
		} 
		else if (0 == strcmp(font_type,BWENC_FONT_KELSON)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_KELSON_T;
		} 
		else if (0 == strcmp(font_type,BWENC_FONT_BITTER)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_BITTER_T;
		} 
		else if (0 == strcmp(font_type,BWENC_FONT_ZWODERI)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_ZWODERI_T;
		} 
		else if (0 == strcmp(font_type,BWENC_FONT_BRAXTON)) 
		{
			sub_info->sub_fontstyle = BWENC_FONT_BRAXTON_T;
		}

		std::string font_cl = font_color;
		sub_info->sub_fontcolor = BWStr::colorstrtoint(font_cl);
		std::string back_cl = back_color;
		sub_info->sub_backcolor = BWStr::colorstrtoint(back_cl);
		
		sub_info->sub_x = p_x;
		sub_info->sub_y = p_y;
		sub_info->sub_width = dis_w;
		sub_info->sub_height = dis_h;
		
		if (0 == strcmp(roll_speed,BWENC_RS_SLOW)) 
		{
			sub_info->sub_movspeed = BWENC_RS_SLOW_T;
		} 
		else if (0 == strcmp(roll_speed,BWENC_RS_NORMAL)) 
		{
			sub_info->sub_movspeed = BWENC_RS_NORMAL_T;
		} 
		else if (0 == strcmp(roll_speed,BWENC_RS_QUICK)) 
		{
			sub_info->sub_movspeed = BWENC_RS_QUICK_T;
		}

		//生效时间
		for(int d = 0; d < ENC_WEEK_DAYS; d++)
		{
			snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
										DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_LEVEL,level,DBCOL_DAY, d,DBCOL_STATE);

			BWRows period_rows = db.QeuryRows(sql);
			if(period_rows.empty() || period_rows.size()==0)
			{
				continue;
			}
			
			for (unsigned int t = 0; t < period_rows.size(); t++) 
			{
				if(t >= ENC_OSD_MAX_TM_NUM)
				{
					break;
				}
				
				BWRow p_row = period_rows[t];

				(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
				(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
			}
		}

		chl_info->encchl_setvsion++;
	}

	snprintf(logstr,sizeof(logstr),"slot:%d-ch:%d OSD(Subtitle) set successfully",s_id,c_id);
	log_write(BWENC_LOG_SYS_SET, logstr);
	
	cJSON_Delete(root);
	return bwenc_common_ack(code);
}

// QR code生成/编辑
const char* qrcode_edit(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;

	int chl_id = (s_id - 1) * c_id;

	char cmd_buf[256]={0};
	char sql[SZ_1K] = { 0 };
	
	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);
	
	
	p_tmp = cJSON_GetObjectItem(param, "vd_format");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0) {
		cgi_debug("vd_format error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int vd_format = std::stoi(p_tmp->valuestring);
	if (vd_format < BWENC_VD_1920 || vd_format > BWENC_VD_480) {
		cgi_debug("vd_format value error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	p_tmp = cJSON_GetObjectItem(param, "level");
	if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint >= ENC_OSD_MAX_OVERLAY) {
		cgi_debug("level error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int level = p_tmp->valueint;

	int opacity = 0;
	p_tmp = cJSON_GetObjectItem(param, "opacity");
	if (p_tmp) {
		if (p_tmp->valueint < 0 || p_tmp->valueint > 10) {
			cgi_debug("opacity error");
			code = BWENC_ERROR_INVALID_PARAM;
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		opacity = p_tmp->valueint;
	}

	// width
	p_tmp = cJSON_GetObjectItem(param, "width");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("width error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int width = p_tmp->valueint;

	// height
	p_tmp = cJSON_GetObjectItem(param, "height");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("height error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int height = p_tmp->valueint;

	// dis_x
	p_tmp = cJSON_GetObjectItem(param, "dis_x");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
		cgi_debug("dis_x error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int dis_x = p_tmp->valueint;

	// dis_y
	p_tmp = cJSON_GetObjectItem(param, "dis_y");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
		cgi_debug("dis_y error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int dis_y = p_tmp->valueint;

	// font
	p_tmp = cJSON_GetObjectItem(param, "font");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| (strcmp(p_tmp->valuestring, BWENC_FONT_AILERON) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_IDEALIST) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_KELSON) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_BITTER) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_ZWODERI) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_BRAXTON) != 0)) {
		cgi_debug("font error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char font[20] = { 0 };
	memcpy(font, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// font_size
	p_tmp = cJSON_GetObjectItem(param, "font_size");
	if (!p_tmp || p_tmp->valueint < BWENC_FONT_SIZE_MIN
			|| p_tmp->valueint > BWENC_FONT_SIZE_MAX) {
		cgi_debug("font_size error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	//int font_size = p_tmp->valueint;

	// font_type
	p_tmp = cJSON_GetObjectItem(param, "font_type");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| (strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_NORMAL) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_BOLD) != 0
					&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_ITALIC) != 0)) {
		cgi_debug("font_type error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char font_type[10] = { 0 };
	memcpy(font_type, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// font_color
	p_tmp = cJSON_GetObjectItem(param, "font_color");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
			|| strlen(p_tmp->valuestring) > 7) {
		cgi_debug("font_color error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char font_color[7] = { 0 };
	memcpy(font_color, p_tmp->valuestring, strlen(p_tmp->valuestring));

	p_tmp = cJSON_GetObjectItem(param, "text_p");
	if (!p_tmp
			|| (p_tmp->valueint != BWENC_TEXT_POS_UP
					&& p_tmp->valueint != BWENC_TEXT_POS_DOWN)) {
		cgi_debug("text_p error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	//int text_p = p_tmp->valueint;

	//qrcode url
	p_tmp = cJSON_GetObjectItem(param, "qr_url");
	if (!p_tmp || !p_tmp->valuestring
			|| strlen(p_tmp->valuestring)
					<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
		cgi_debug("url error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char qr_url[BWENC_SUB_LEN_MAX] = { '\0' };
	memcpy(qr_url, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// text
	p_tmp = cJSON_GetObjectItem(param, "text");
	if (!p_tmp || !p_tmp->valuestring
			|| strlen(p_tmp->valuestring)
					<= 0||strlen(p_tmp->valuestring)>BWENC_QR_TEXT_LEN_MAX) {
		cgi_debug("text error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char text[BWENC_QR_TEXT_LEN_MAX] = { '\0' };
	memcpy(text, p_tmp->valuestring, strlen(p_tmp->valuestring));

	// text_url
	p_tmp = cJSON_GetObjectItem(param, "text_url");
	if (!p_tmp || !p_tmp->valuestring
			|| strlen(p_tmp->valuestring)
					<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
		cgi_debug("logo_url error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	char text_url[BWENC_SUB_LEN_MAX] = { '\0' };
	memcpy(text_url, p_tmp->valuestring, strlen(p_tmp->valuestring));

	/* qrcode_data
	p_tmp = cJSON_GetObjectItem(param, "qrcode_data");
	if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0) {
		cgi_debug("qrcode_data error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	Base64 b64;
	//	std::string str =

	char filename[50] = { '\0' };
	//b64.dec2Image(p_tmp->valuestring, filename);
	//	cgi_debug("%s",filename);
	*/

	// period
	p_tmp = cJSON_GetObjectItem(param, "period");
	if (!p_tmp) {
		cgi_debug("no period error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	cJSON *pe_item = NULL;
	unsigned int pe_num = cJSON_GetArraySize(p_tmp);
	if (pe_num <= 0) {
		cgi_debug("parse period array num error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	int ret = db.Exec("begin;");	// 开启事务
	if (SQLITE_OK != ret) {
		cgi_debug("start transaction error");
		cJSON_Delete(root);
		return bwenc_common_ack(BWENC_ERROR_DB_ERR);
	}

	DateTime dt;
	char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
	dt.time2string(time(nullptr), timeStr);
	
	cJSON* period_tmp = NULL;
	for (unsigned int i = 0; i < pe_num; i++) {
		pe_item = cJSON_GetArrayItem(p_tmp, i);
		period_tmp = cJSON_GetObjectItem(pe_item, "day");
		if (!period_tmp || period_tmp->valueint < 0
				|| period_tmp->valueint > 6) {
			cgi_debug("period day error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		int day = period_tmp->valueint;

		// start_time
		period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
		DateTime dt;
		if (!period_tmp || !period_tmp->valuestring
				|| strlen(period_tmp->valuestring) != 8
				|| !dt.timeCheck(period_tmp->valuestring)) {
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		char start_time[10] = { 0 };
		memcpy(start_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// end_time
		period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
		if (!period_tmp || !period_tmp->valuestring
				|| strlen(period_tmp->valuestring) != 8
				|| !dt.timeCheck(period_tmp->valuestring)) {
			cgi_debug("period start_time error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		char end_time[10] = { 0 };
		memcpy(end_time, period_tmp->valuestring,
				strlen(period_tmp->valuestring));

		// state
		int state = 0;
		period_tmp = cJSON_GetObjectItem(pe_item, "state");
		if (!period_tmp
				|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
			cgi_debug("period state error");
			code = BWENC_ERROR_INVALID_PARAM;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		state = period_tmp->valueint;

		//先删除原来的
		snprintf(sql, sizeof(sql),
				"delete from %s where %s=%d and %s=%d",
				DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_LEVEL,level);
		if (SQLITE_OK != db.Exec(sql)) 
		{
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}
		
		// 插入period数据
		snprintf(sql, sizeof(sql),
				"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
				DBTBL_OSD_PERIOD, 
				DBCOL_CHL_ID, DBCOL_LEVEL, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
				chl_id, level, day, start_time, end_time,state);
		
		if (SQLITE_OK != db.Exec(sql)) {
			cgi_debug("insert sb_set  error");
			code = BWENC_ERROR_DB_ERR;
			db.Exec("rollback;");
			cJSON_Delete(root);
			return bwenc_common_ack(code);
		}

	}

	
	db.Exec("commit;");
	code = BWENC_ERROR_NONE;

	//next_show_osd();
//设置到共享内存	
	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;
	stENC_QRCODE* qr_info = NULL;
	
	if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
	{
		chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
		osd_info = &(chl_info->encchl_osd[level]);

		//清空
		memset(osd_info,0,sizeof(stENC_OSDSET));
		
		osd_info->osd_enable = 1;
		osd_info->osd_type = BWENC_OSD_TYPE_QRCODE;

		qr_info = &(osd_info->osd_cfg.osd_qr);
		
		snprintf((char *)qr_info->qr_url, sizeof(qr_info->qr_url),"%s",qr_url);
		qr_info->qr_opacity = opacity;
		qr_info->qr_width = width;
		qr_info->qr_height = height;
		qr_info->qr_x = dis_x;
		qr_info->qr_y = dis_y;
		

		//生效时间
		for(int d = 0; d < ENC_WEEK_DAYS; d++)
		{
			snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
										DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_LEVEL,level,DBCOL_DAY, d,DBCOL_STATE);

			BWRows period_rows = db.QeuryRows(sql);
			if(period_rows.empty() || period_rows.size()==0)
			{
				continue;
			}
			
			for (unsigned int t = 0; t < period_rows.size(); t++) 
			{
				if(t >= ENC_OSD_MAX_TM_NUM)
				{
					break;
				}
				
				BWRow p_row = period_rows[t];

				(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
				(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
			}
		}

		chl_info->encchl_setvsion++;
	}

	snprintf(logstr,sizeof(logstr),"slot:%d-ch:%d OSD(QR code) modified successfully",s_id,c_id);
	log_write(BWENC_LOG_SYS_SET, logstr);
	

	cJSON_Delete(root);
	return bwenc_common_ack(code);
}

// OSD获取
#if 0
const char* osd_list(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;
	cJSON_Delete(root);

//*************************
//get osd info
//
	BWSqlite db(BWENC_DATABASE);
	char sql[SZ_1K] = { 0 };
	snprintf(sql, sizeof(sql), "SELECT * FROM %s order by id desc", DBTBL_OSD_SET);
	BWRows rows = db.QeuryRows(sql);

	// 构造返回json
	cJSON *pout_root = NULL;
	cJSON *p_obj = NULL;
	cJSON *p_arr = NULL;
	cJSON *arr_tmp = NULL;

	pout_root = cJSON_CreateObject();
	cJSON_AddNumberToObject(pout_root, "retCode", 0);
	cJSON_AddStringToObject(pout_root, "retMsg", "SUCCESS");
	p_obj = cJSON_CreateObject();
	cJSON_AddItemToObject(pout_root, "retData", p_obj);
	p_arr = cJSON_CreateArray();
	cJSON_AddItemToObject(p_obj, "osd_list", p_arr);

	if (!rows.empty()) 
	{
		for (std::vector<BWRow>::size_type i = 0; i < rows.size(); i++) 
		{
			BWRow temRow = rows[i];
			arr_tmp = cJSON_CreateObject();
			cJSON_AddItemToArray(p_arr, arr_tmp);

			cJSON_AddStringToObject(arr_tmp, "id", temRow[DBCOL_ID].c_str());

			cJSON_AddStringToObject(arr_tmp, "chl_id", temRow[DBCOL_CHL_ID].c_str());
			std::string type_str = temRow[DBCOL_TYPE];
			int type = atoi(type_str.c_str());
			cJSON_AddNumberToObject(arr_tmp, "type", type);
			const char* c_id = temRow[DBCOL_C_ID].c_str();
			cJSON_AddStringToObject(arr_tmp, "c_id", c_id);
			cJSON_AddStringToObject(arr_tmp, "level",temRow[DBCOL_LEVEL].c_str());
			
			std::string vd_format = "";
			if (type == BWENC_OSD_TYPE_TEXT) 
			{
				snprintf(sql, sizeof(sql), "SELECT * FROM %s where %s=%s",
				DBTBL_SUB_SET, DBCOL_ID, c_id);
				BWRow text_row = db.QueryRow(sql);
				if (!text_row.empty()) 
				{
					vd_format = text_row[DBCOL_VD_FORMAT];

					cJSON_AddStringToObject(arr_tmp, "roll_speed",
							text_row[DBCOL_ROLL_SPEED].c_str());
					cJSON_AddStringToObject(arr_tmp, "dis_w",
							text_row[DBCOL_DIS_W].c_str());
					cJSON_AddStringToObject(arr_tmp, "dis_h",
							text_row[DBCOL_DIS_H].c_str());
					cJSON_AddStringToObject(arr_tmp, "p_x",
							text_row[DBCOL_P_X].c_str());
					cJSON_AddStringToObject(arr_tmp, "p_y",
							text_row[DBCOL_P_Y].c_str());
					cJSON_AddStringToObject(arr_tmp, "font",
							text_row[DBCOL_FONT].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_size",
							text_row[DBCOL_FONT_SIZE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_type",
							text_row[DBCOL_FONT_TYPE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_color",
							text_row[DBCOL_FONT_COLOR].c_str());
					cJSON_AddStringToObject(arr_tmp, "back_color",
							text_row[DBCOL_BACK_COLOR].c_str());
					cJSON_AddStringToObject(arr_tmp, "content",
							text_row[DBCOL_CONTENT].c_str());
				}
			} 
			else if (type == BWENC_OSD_TYPE_LOGO) 
			{
				snprintf(sql, sizeof(sql), "SELECT * FROM %s where %s=%s",
				DBTBL_LOGO_SET, DBCOL_ID, c_id);
				BWRow logo_row = db.QueryRow(sql);
				if (!logo_row.empty()) 
				{
					vd_format = logo_row[DBCOL_VD_FORMAT];

					cJSON_AddStringToObject(arr_tmp, "slide_interval",
							logo_row[DBCOL_SLIDE_INTERVAL].c_str());
					cJSON_AddStringToObject(arr_tmp, "opacity",
							logo_row[DBCOL_OPACITY].c_str());
					cJSON_AddStringToObject(arr_tmp, "p_x",
							logo_row[DBCOL_P_X].c_str());
					cJSON_AddStringToObject(arr_tmp, "p_y",
							logo_row[DBCOL_P_Y].c_str());
					cJSON_AddStringToObject(arr_tmp, "width",
							logo_row[DBCOL_WIDTH].c_str());
					cJSON_AddStringToObject(arr_tmp, "height",
							logo_row[DBCOL_HEIGHT].c_str());

					// 获取logo的img数组
					cJSON *img_arr = cJSON_CreateArray();
					cJSON_AddItemToObject(arr_tmp, "img_url", img_arr);
					cJSON *img_obj = NULL;

					snprintf(sql, sizeof(sql),
							"SELECT * FROM %s where %s=%s order by id desc",
							DBTBL_LOGO_IMG, DBCOL_L_ID,
							logo_row[DBCOL_ID].c_str());
					BWRows img_rows = db.QeuryRows(sql);
					if (!img_rows.empty()) {
						for (std::vector<BWRow>::size_type j = 0;
								j < img_rows.size(); j++) {
							img_obj = cJSON_CreateObject();
							cJSON_AddItemToArray(img_arr, img_obj);
							cJSON_AddStringToObject(img_obj, "url",
									img_rows[j][DBCOL_URL].c_str());
						}
					}
				}
			} 
			else if (BWENC_OSD_TYPE_QRCODE) 
			{
				snprintf(sql, sizeof(sql), "SELECT * FROM %s where %s=%s",
				DBTBL_QRCODE_SET, DBCOL_ID, c_id);
				BWRow qr_row = db.QueryRow(sql);
				if (!qr_row.empty()) {
					vd_format = qr_row[DBCOL_VD_FORMAT];
					cJSON_AddStringToObject(arr_tmp, "opacity",
							qr_row[DBCOL_OPACITY].c_str());
					cJSON_AddStringToObject(arr_tmp, "dis_x",
							qr_row[DBCOL_DIS_X].c_str());
					cJSON_AddStringToObject(arr_tmp, "dis_y",
							qr_row[DBCOL_DIS_Y].c_str());
					cJSON_AddStringToObject(arr_tmp, "width",
							qr_row[DBCOL_WIDTH].c_str());
					cJSON_AddStringToObject(arr_tmp, "height",
							qr_row[DBCOL_HEIGHT].c_str());
					cJSON_AddStringToObject(arr_tmp, "font",
							qr_row[DBCOL_FONT].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_size",
							qr_row[DBCOL_FONT_SIZE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_type",
							qr_row[DBCOL_FONT_TYPE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_color",
							qr_row[DBCOL_FONT_COLOR].c_str());
					cJSON_AddStringToObject(arr_tmp, "text_p",
							qr_row[DBCOL_TEXT_P].c_str());
					cJSON_AddStringToObject(arr_tmp, "text",
							qr_row[DBCOL_TEXT].c_str());
					cJSON_AddStringToObject(arr_tmp, "url",
							qr_row[DBCOL_URL].c_str());
					cJSON_AddStringToObject(arr_tmp, "logo_url",
							qr_row[DBCOL_LOGO_URL].c_str());
					cJSON_AddStringToObject(arr_tmp, "img_url",
							qr_row[DBCOL_IMG_URL].c_str());
				}
			}

			// vd_format处理
			cJSON_AddStringToObject(arr_tmp, "vd_format", vd_format.c_str());

			// 获取时间段
			snprintf(sql, sizeof(sql),
					"SELECT * FROM %s where %s=%s order by id desc",
					DBTBL_OSD_PERIOD, DBCOL_OS_ID, temRow[DBCOL_ID].c_str());
			BWRows p_rows = db.QeuryRows(sql);
			cJSON *p_arr = cJSON_CreateArray();
			cJSON_AddItemToObject(arr_tmp, "period", p_arr);
			cJSON *p_obj = NULL;
			if (!p_rows.empty()) 
			{
				for (std::vector<BWRow>::size_type j = 0; j < p_rows.size();
						j++) {
					p_obj = cJSON_CreateObject();
					cJSON_AddItemToArray(p_arr, p_obj);
					BWRow p_row = p_rows[j];
					cJSON_AddStringToObject(p_obj, "day",
							p_row[DBCOL_DAY].c_str());
					cJSON_AddStringToObject(p_obj, "day",
							p_row[DBCOL_DAY].c_str());
					cJSON_AddStringToObject(p_obj, "start_time",
							p_row[DBCOL_START_TIME].c_str());
					cJSON_AddStringToObject(p_obj, "end_time",
							p_row[DBCOL_END_TIME].c_str());
					cJSON_AddStringToObject(p_obj, "state",
							p_row[DBCOL_STATE].c_str());
				}
			}

			// 获取chl_id
			snprintf(sql, sizeof(sql),
					"SELECT * FROM %s where %s=%s order by rowid desc",
					DBTBL_OSD_CHL, DBCOL_OS_ID, temRow[DBCOL_ID].c_str());
			
			BWRows c_rows = db.QeuryRows(sql);
			cJSON *c_arr = cJSON_CreateArray();
			cJSON *c_str = NULL;
			cJSON_AddItemToObject(arr_tmp, "chl_id", c_arr);
			
			if (!c_rows.empty()) 
			{
				for (std::vector<BWRow>::size_type j = 0; j < c_rows.size();j++) 
				{
					c_str = cJSON_CreateString(c_rows[j][DBCOL_CHL_ID].c_str());
					cJSON_AddItemToArray(c_arr, c_str);
				}
			}
		}
	}
	
	jsonMsgtoStr(pout_root,g_outbuf,get_g_oubuf_size());

	cJSON_Delete(pout_root);
	return g_outbuf;
}
#else
const char* osd_list(const char* json) 
{
	//先解析请求
	//char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;
	int chl_id = (s_id -1 ) * 4 +  c_id;

//*************************
//get osd info
	char cmd_buf[256]={0};
	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);	
	char sql[SZ_1K] = { 0 };

	// 构造返回json
	cJSON *pout_root = NULL;
	cJSON *p_obj = NULL;
	cJSON *p_arr = NULL;
	cJSON *arr_tmp = NULL;

	pout_root = cJSON_CreateObject();
	cJSON_AddNumberToObject(pout_root, "retCode", 0);
	cJSON_AddStringToObject(pout_root, "retMsg", "SUCCESS");
	p_obj = cJSON_CreateObject();
	cJSON_AddItemToObject(pout_root, "retData", p_obj);
	p_arr = cJSON_CreateArray();
	cJSON_AddItemToObject(p_obj, "osd_list", p_arr);

	//一个通道3个osd
	stENC_CHLSET *chl_info = NULL;
	stENC_OSDSET *osd_info = NULL;
	stENC_SUBSET *sub_info = NULL;
	stENC_LOGOSET *logo_info = NULL;
	stENC_QRCODE *qr_info = NULL;
	char tmpbuf[256]={0};

	if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
	{
		chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
		
		for (int i = 0; i < ENC_OSD_MAX_OVERLAY;i++) 
		{
			osd_info = &(chl_info->encchl_osd[i]);

			if(0 == osd_info->osd_enable)
			{
				continue;
			}
			
			arr_tmp = cJSON_CreateObject();
			cJSON_AddItemToArray(p_arr, arr_tmp);

			int type = osd_info->osd_type;
			cJSON_AddNumberToObject(arr_tmp, "type", type);

			sprintf(tmpbuf,"%d",i);
			cJSON_AddStringToObject(arr_tmp, "osd_id",tmpbuf);

			sprintf(tmpbuf,"%d",osd_info->priority);
			cJSON_AddStringToObject(arr_tmp, "priority",tmpbuf);
			
			// vd_format处理
			//cJSON_AddStringToObject(arr_tmp, "vd_format", vd_format.c_str());
			
			if (type == BWENC_OSD_TYPE_TEXT) 
			{
				sub_info = &(osd_info->osd_cfg.osd_sub);
				
				sprintf(tmpbuf,"%d",sub_info->sub_movspeed);
				cJSON_AddStringToObject(arr_tmp, "roll_speed",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_width);
				cJSON_AddStringToObject(arr_tmp, "dis_w",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_height);
				cJSON_AddStringToObject(arr_tmp, "dis_h",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_x);
				cJSON_AddStringToObject(arr_tmp, "p_x",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_y);
				cJSON_AddStringToObject(arr_tmp, "p_y",tmpbuf);
				
				cJSON_AddStringToObject(arr_tmp, "font",(char *)sub_info->sub_font);

				sprintf(tmpbuf,"%d",sub_info->sub_fontsize);
				cJSON_AddStringToObject(arr_tmp, "font_size",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_fontstyle);
				cJSON_AddStringToObject(arr_tmp, "font_type",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_fontcolor);
				cJSON_AddStringToObject(arr_tmp, "font_color",tmpbuf);

				sprintf(tmpbuf,"%d",sub_info->sub_backcolor);
				cJSON_AddStringToObject(arr_tmp, "back_color",tmpbuf);
				
				cJSON_AddStringToObject(arr_tmp, "content",(char *)sub_info->sub_content);
				
			} 
			else if (type == BWENC_OSD_TYPE_LOGO) 
			{
				logo_info = &(osd_info->osd_cfg.osd_logo);

				sprintf(tmpbuf,"%d",logo_info->logo_movspeed);
				cJSON_AddStringToObject(arr_tmp, "slide_interval",tmpbuf);

				sprintf(tmpbuf,"%d",logo_info->logo_opacity);
				cJSON_AddStringToObject(arr_tmp, "opacity",tmpbuf);

				sprintf(tmpbuf,"%d",logo_info->logo_x);
				cJSON_AddStringToObject(arr_tmp, "p_x",tmpbuf);
				sprintf(tmpbuf,"%d",logo_info->logo_y);
				cJSON_AddStringToObject(arr_tmp, "p_y",tmpbuf);

				sprintf(tmpbuf,"%d",logo_info->logo_width);
				cJSON_AddStringToObject(arr_tmp, "width",tmpbuf);

				sprintf(tmpbuf,"%d",logo_info->logo_height);
				cJSON_AddStringToObject(arr_tmp, "height",tmpbuf);

				// 获取logo的img数组
				cJSON *img_arr = cJSON_CreateArray();
				cJSON_AddItemToObject(arr_tmp, "img_url", img_arr);
				cJSON *img_obj = NULL;

				for (int j = 0; j < ENC_OSD_MAX_SLIDE_NUM && j < logo_info->logo_num; j++) 
				{
					img_obj = cJSON_CreateObject();
					cJSON_AddItemToArray(img_arr, img_obj);
					snprintf(tmpbuf,sizeof(tmpbuf),"%s",(char *)logo_info->logo_url[j]);
					cJSON_AddStringToObject(img_obj, "url", tmpbuf);
				}
				
			} 
			else if (BWENC_OSD_TYPE_QRCODE) 
			{
				qr_info = &(osd_info->osd_cfg.osd_qr);

				sprintf(tmpbuf,"%d",qr_info->qr_opacity);
				cJSON_AddStringToObject(arr_tmp, "opacity",tmpbuf);

				sprintf(tmpbuf,"%d",qr_info->qr_x);
				cJSON_AddStringToObject(arr_tmp, "dis_x",tmpbuf);

				sprintf(tmpbuf,"%d",qr_info->qr_y);
				cJSON_AddStringToObject(arr_tmp, "dis_y",tmpbuf);

				sprintf(tmpbuf,"%d",qr_info->qr_width);
				cJSON_AddStringToObject(arr_tmp, "width",tmpbuf);

				sprintf(tmpbuf,"%d",qr_info->qr_height);
				cJSON_AddStringToObject(arr_tmp, "height",tmpbuf);
						
				cJSON_AddStringToObject(arr_tmp, "qr_url",(char *)qr_info->qr_url);

				//获取 额外信息
				snprintf(sql, sizeof(sql), "SELECT * FROM %s where %s=%d and %s=%d",
				DBTBL_QRCODE_SET, DBCOL_CHL_ID,chl_id,DBCOL_OSD_ID, i);
				BWRow qr_row = db.QueryRow(sql);
				if (!qr_row.empty()) 
				{
					cJSON_AddStringToObject(arr_tmp, "font",
							qr_row[DBCOL_FONT].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_size",
							qr_row[DBCOL_FONT_SIZE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_type",
							qr_row[DBCOL_FONT_TYPE].c_str());
					cJSON_AddStringToObject(arr_tmp, "font_color",
							qr_row[DBCOL_FONT_COLOR].c_str());
					cJSON_AddStringToObject(arr_tmp, "text_p",
							qr_row[DBCOL_TEXT_P].c_str());
					cJSON_AddStringToObject(arr_tmp, "text",
							qr_row[DBCOL_TEXT].c_str());
					cJSON_AddStringToObject(arr_tmp, "text_url",
							qr_row[DBCOL_TEXT_URL].c_str());
					cJSON_AddStringToObject(arr_tmp, "img_url",
							qr_row[DBCOL_IMG_URL].c_str());		
				}
				
			}

			// 获取时间段
			cJSON *p_arr = cJSON_CreateArray();
			cJSON_AddItemToObject(arr_tmp, "period", p_arr);
			cJSON *p_obj = NULL;
			
			snprintf(sql, sizeof(sql),
					"SELECT * FROM %s where %s=%d and %s=%d order by id desc",
					DBTBL_OSD_PERIOD, DBCOL_CHL_ID, chl_id,DBCOL_OSD_ID,i);
			
			BWRows p_rows = db.QeuryRows(sql);
			if (!p_rows.empty()) 
			{
				for (std::vector<BWRow>::size_type tt = 0; tt < p_rows.size();tt++) 
				{
					p_obj = cJSON_CreateObject();
					cJSON_AddItemToArray(p_arr, p_obj);
					
					BWRow p_row = p_rows[tt];
					cJSON_AddStringToObject(p_obj, "day",p_row[DBCOL_DAY].c_str());
					
					cJSON_AddStringToObject(p_obj, "day",p_row[DBCOL_DAY].c_str());
					
					cJSON_AddStringToObject(p_obj, "start_time",p_row[DBCOL_START_TIME].c_str());
					
					cJSON_AddStringToObject(p_obj, "end_time",p_row[DBCOL_END_TIME].c_str());
					
					cJSON_AddStringToObject(p_obj, "state",p_row[DBCOL_STATE].c_str());
				}
			}
		}
	}
	
	jsonMsgtoStr(pout_root,g_outbuf,get_g_oubuf_size());

	cJSON_Delete(root);
	cJSON_Delete(pout_root);
	return g_outbuf;
}

#endif


// OSD-删除
const char* osd_del(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_UNKNOW;
	
	cJSON* root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	cJSON* param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	cJSON *p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int c_id = p_tmp->valueint;
	int chl_id = (s_id -1 ) * 4 +  c_id;

	p_tmp = cJSON_GetObjectItem(param, "level");
	if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint >= ENC_OSD_MAX_OVERLAY) 
	{
		cgi_debug("level error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	int level = p_tmp->valueint;

//获取记录总数；
	/*
	if (type == BWENC_OSD_TYPE_TEXT) 
	{
		
	} 
	else if (type == BWENC_OSD_TYPE_LOGO) 
	{
		
	} 
	else if (type == BWENC_OSD_TYPE_QRCODE) 
	{
		
	}*/

	//将需要删除的通道设置 到共享内存；
	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;

	
	if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
	{
		chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
		osd_info = &(chl_info->encchl_osd[level]);
		osd_info->osd_enable = 0;
		memset(&(osd_info->osd_cfg),0,sizeof(osd_info->osd_cfg));
				
		//生效时间
		for(int d = 0; d < ENC_WEEK_DAYS; d++)
		{
			for (unsigned int t = 0; t < ENC_OSD_MAX_TM_NUM; t++) 
			{
				osd_info->osd_effect_tm[d][t].tm_start_sec = 0;
				osd_info->osd_effect_tm[d][t].tm_end_sec = 0;
			}
		}

		chl_info->encchl_setvsion++;
	}


	log_write(BWENC_LOG_SYS_SET, logstr);

	cJSON_Delete(root);	
	return bwenc_common_ack(BWENC_ERROR_NONE);
}


#if 1
/**
 * method:设置下一个生效的osd到共享内存，共享内存显示完一个osd调用方法获取下一个生效的osd
 * params:取所有通道、所有层的
 * return:返回0表示成功，-1表示失败
 */
int next_show_osd() 
{

	BWSqlite db(BWENC_DATABASE);
	char sql[SZ_1K] = { 0 };

	time_t now;
	struct tm *tm_now;
	time(&now);
	tm_now = localtime(&now);
	unsigned short day = tm_now->tm_wday - 1;
	char timeNow[8] = { '\0' };
	sprintf(timeNow, "%d:%d:%d", tm_now->tm_hour, tm_now->tm_min,tm_now->tm_sec);
	
	snprintf(sql, sizeof(sql),
			"SELECT * FROM %s as a JOIN %s as b ON a.os_id = b.os_id WHERE %s=1 and %s=%d and %s>'%s' group by chl_id,level order by start_time asc,%s asc",
			DBTBL_OSD_PERIOD, DBTBL_OSD_CHL, DBCOL_STATE, DBCOL_DAY, day,
			DBCOL_END_TIME, timeNow, DBCOL_UP_TIME);

	BWRows period_rows = db.QeuryRows(sql);
	if (period_rows.empty() || period_rows.size() <= 0) 
	{
		// 如果没有当天的数据，拿大于当天的最早的一个作为下一生效时间段
		cgi_debug("no period,day：%d,time:%s", day, timeNow);
		snprintf(sql, sizeof(sql),
				"SELECT * FROM %s as a JOIN %s as b on a.os_id = b.os_id WHERE %s=1 and %s>%d group by chl_id,level order by %s asc,%s asc,%s asc",
				DBTBL_OSD_PERIOD, DBTBL_OSD_CHL, DBCOL_STATE, DBCOL_DAY, day,
				DBCOL_DAY, DBCOL_START_TIME, DBCOL_UP_TIME);
		
		period_rows = db.QeuryRows(sql);
		if (period_rows.empty() || period_rows.size() <= 0) 
		{
			// 没有大于当天的数据，拿小于当天的最早的一个作为下一生效时间段
			cgi_debug("no period1,day：%d", day);
			snprintf(sql, sizeof(sql),
					"SELECT * FROM %s a JOIN %s as b on a.os_id = b.os_id WHERE %s=1 and %s<=%d group by chl_id,level order by %s asc,%s asc,%s asc",
					DBTBL_OSD_PERIOD, DBTBL_OSD_CHL, DBCOL_STATE, DBCOL_DAY,
					day, DBCOL_DAY, DBCOL_START_TIME, DBCOL_UP_TIME);
			
			period_rows = db.QeuryRows(sql);
			if (period_rows.empty() || period_rows.size() <= 0) 
			{
				cgi_debug("no period2,day：%d", day);
				return 0;
			}
		}
	}

	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;
	stENC_LOGOSET* logo_info = NULL;
	stENC_SUBSET* sub_info = NULL;
	stENC_QRCODE* qr_info = NULL;
	DateTime dt;

	sem_wait(g_shrmsem);
	for (unsigned int i = 0; i < period_rows.size(); i++) 
	{
		BWRow p_row = period_rows[i];
	
		int chl_id = std::stoi(p_row[DBCOL_CHL_ID]);
		chl_id--;
		chl_info = &(g_encset_mst->enc_chls[chl_id]);
		int level = std::stoi(p_row[DBCOL_LEVEL]);
		
		osd_info = &(chl_info->encchl_osd[level]);
		osd_info->osd_enable = 1;
		
		memset(osd_info->osd_effect_tm, 0, sizeof(osd_info->osd_effect_tm));
		int day = std::stoi(p_row[DBCOL_DAY]);
		
		(osd_info->osd_effect_tm)[day][0].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
		(osd_info->osd_effect_tm)[day][0].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
		
		std::string type_str = p_row[DBCOL_TYPE];
		int type = atoi(type_str.c_str());
		osd_info->osd_type = type;
		
		if (type == BWENC_OSD_TYPE_TEXT) 
		{
			sub_info = &(osd_info->osd_cfg.osd_sub);
			snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s=%s",DBTBL_SUB_SET, DBCOL_ID, p_row[DBCOL_C_ID].c_str());
			
			BWRow sub_row = db.QueryRow(sql);
			if (sub_row.empty()) 
			{
				cgi_debug("sub null");
				continue;
			}
			memcpy(sub_info->sub_content, sub_row[DBCOL_CONTENT].c_str(),strlen(sub_row[DBCOL_CONTENT].c_str()));
			memcpy(sub_info->sub_font, sub_row[DBCOL_FONT].c_str(),strlen(sub_row[DBCOL_FONT].c_str()));
			
			sub_info->sub_fontsize = std::stoi(sub_row[DBCOL_FONT_SIZE]);
			if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_AILERON)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_AILERON_T;
			} 
			else if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_IDEALIST)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_IDEALIST_T;
			} 
			else if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_KELSON)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_KELSON_T;
			} 
			else if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_BITTER)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_BITTER_T;
			} 
			else if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_ZWODERI)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_ZWODERI_T;
			} 
			else if (0 == sub_row[DBCOL_FONT_TYPE].compare(BWENC_FONT_BRAXTON)) 
			{
				sub_info->sub_fontstyle = BWENC_FONT_BRAXTON_T;
			}
			
			sub_info->sub_fontcolor = BWStr::colorstrtoint(sub_row[DBCOL_FONT_COLOR]);
			sub_info->sub_backcolor = BWStr::colorstrtoint(sub_row[DBCOL_BACK_COLOR]);
			
			sub_info->sub_x = std::stoi(sub_row[DBCOL_P_X]);
			sub_info->sub_y = std::stoi(sub_row[DBCOL_P_Y]);
			sub_info->sub_width = std::stoi(sub_row[DBCOL_DIS_W]);
			sub_info->sub_height = std::stoi(sub_row[DBCOL_DIS_H]);
			
			if (0 == sub_row[DBCOL_ROLL_SPEED].compare(BWENC_RS_SLOW)) 
			{
				sub_info->sub_movspeed = BWENC_RS_SLOW_T;
			} 
			else if (0 == sub_row[DBCOL_ROLL_SPEED].compare(BWENC_RS_NORMAL)) 
			{
				sub_info->sub_movspeed = BWENC_RS_NORMAL_T;
			} 
			else if (0 == sub_row[DBCOL_ROLL_SPEED].compare(BWENC_RS_QUICK)) 
			{
				sub_info->sub_movspeed = BWENC_RS_QUICK_T;
			}
		} 
		else if (type == BWENC_OSD_TYPE_QRCODE) 
		{
			qr_info = &(osd_info->osd_cfg.osd_qr);
			snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s=%s",
			DBTBL_QRCODE_SET, DBCOL_ID, p_row[DBCOL_C_ID].c_str());
			BWRow qr_row = db.QueryRow(sql);
			if (qr_row.empty()) {
				cgi_debug("qr null");
				continue;
			}
			memcpy(qr_info->qr_url, qr_row[DBCOL_URL].c_str(),
					strlen(qr_row[DBCOL_URL].c_str()));
			qr_info->qr_opacity = std::stoi(qr_row[DBCOL_OPACITY]);
			qr_info->qr_width = std::stoi(qr_row[DBCOL_WIDTH]);
			qr_info->qr_height = std::stoi(qr_row[DBCOL_HEIGHT]);
			qr_info->qr_x = std::stoi(qr_row[DBCOL_DIS_X]);
			qr_info->qr_y = std::stoi(qr_row[DBCOL_DIS_Y]);
		} else if (type == BWENC_OSD_TYPE_LOGO) {
			logo_info = &(osd_info->osd_cfg.osd_logo);
			snprintf(sql, sizeof(sql), "SELECT * FROM %s WHERE %s=%s",
			DBTBL_LOGO_SET, DBCOL_ID, p_row[DBCOL_C_ID].c_str());
			BWRow logo_row = db.QueryRow(sql);
			if (logo_row.empty()) {
				cgi_debug("logo null");
				continue;
			}
			logo_info->logo_opacity = std::stoi(logo_row[DBCOL_OPACITY]);
			logo_info->logo_width = std::stoi(logo_row[DBCOL_WIDTH]);
			logo_info->logo_height = std::stoi(logo_row[DBCOL_HEIGHT]);
			logo_info->logo_x = std::stoi(logo_row[DBCOL_P_X]);
			logo_info->logo_y = std::stoi(logo_row[DBCOL_P_Y]);
			snprintf(sql, sizeof(sql),
					"SELECT * FROM %s WHERE %s=%s order by create_tm asc limit 3",
					DBTBL_LOGO_IMG, DBCOL_L_ID, logo_row[DBCOL_ID].c_str());
			BWRows imgs = db.QeuryRows(sql);
			if (imgs.size() <= 0) {
				cgi_debug("img null");
				continue;
			}
			memset(logo_info->logo_url, 0, sizeof(logo_info->logo_url));
			for (unsigned int j = 0; j < imgs.size(); j++) {
				BWRow img = imgs[j];
				memcpy(logo_info->logo_url[j], img[DBCOL_URL].c_str(),
						strlen(img[DBCOL_URL].c_str()));
			}
			logo_info->logo_num = imgs.size();
		}

		chl_info->encchl_setvsion++;
	}
	sem_post(g_shrmsem);
	
	return 0;
}
#endif

//通道一次编辑多个
const char* osd_edit(const char* json) 
{
	//先解析请求
	char logstr[SZ_1K] = {0};
	eENC_ERROR code = BWENC_ERROR_NONE;
	cJSON* root = NULL;
	cJSON* param = NULL;
	cJSON *p_tmp = NULL;
	cJSON *p_osdlist = NULL;
	cJSON *p_osd = NULL;
	
	int chl_id = 0;
	int s_id = 0;
	int c_id = 0;
	int osd_num = 0;
	char cmd_buf[256]={0};
	char sql[SZ_1K] = { 0 };
	int i = 0;
	int osd_type = 0;

	stENC_CHLSET* chl_info = NULL;
	stENC_OSDSET* osd_info = NULL;
	stENC_LOGOSET* logo_info = NULL;	
	stENC_SUBSET* sub_info = NULL;	
	stENC_QRCODE* qr_info = NULL;

	unsigned int priority = 0;

	snprintf(cmd_buf,sizeof(cmd_buf),"%s/%s",FCGI_module_folder(),BWENC_DATABASE);
	BWSqlite db(cmd_buf);
	
	root = cJSON_Parse(json);
	if (!root) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		return bwenc_common_ack(code);
	}

	param = cJSON_GetObjectItem(root, KEY_PARAM);
	if (!param) 
	{
		cgi_debug("error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}

	// s_id
	p_tmp = cJSON_GetObjectItem(param, "s_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_SUB_BOARD) 
	{
		cgi_debug("s_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	s_id = p_tmp->valueint;

	// c_id
	p_tmp = cJSON_GetObjectItem(param, "c_id");
	if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > ENC_MAX_CHL) 
	{
		cgi_debug("c_id error");
		code = BWENC_ERROR_INVALID_PARAM;
		cJSON_Delete(root);
		return bwenc_common_ack(code);
	}
	c_id = p_tmp->valueint;
	chl_id = (s_id - 1) * 4 + c_id;

	//获取osdlist
	p_osdlist = cJSON_GetObjectItem(param, "osd_list");
	osd_num  = cJSON_GetArraySize(p_osdlist);
	cgi_debug("osd num:%d",osd_num);

	chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
	memset(chl_info->encchl_osd,0,sizeof(stENC_OSDSET) * ENC_OSD_MAX_OVERLAY);
	
	for(i = 0; i < osd_num && i < ENC_OSD_MAX_OVERLAY; i++)
	{
		p_osd = cJSON_GetArrayItem(p_osdlist, i);
		if(p_osd == NULL)
		{
			continue;
		}

		p_tmp = cJSON_GetObjectItem(p_osd, "osd_type");
		osd_type = p_tmp->valueint;
		cgi_debug("osd type:%d",osd_type);

		if(osd_type == BWENC_OSD_TYPE_LOGO)
		{
			p_tmp = cJSON_GetObjectItem(p_osd, "priority");
			priority = p_tmp->valueint > 7 ? 7 : p_tmp->valueint;
			
			p_tmp = cJSON_GetObjectItem(p_osd, "slide_interval");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_OSD_SLIDE_INTERVAL_MAX) 
			{
				cgi_debug("slide_interval error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int slide_interval = p_tmp->valueint;

			p_tmp = cJSON_GetObjectItem(p_osd, "opacity");//空间透明度
			if (!p_tmp || p_tmp->valueint < 0 || p_tmp->valueint > 128) 
			{
				cgi_debug("opacity error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int opacity = p_tmp->valueint;

			// width
			p_tmp = cJSON_GetObjectItem(p_osd, "width");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("width error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int width = p_tmp->valueint;

			// height
			p_tmp = cJSON_GetObjectItem(p_osd, "height");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("height error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int height = p_tmp->valueint;

			// p_x
			p_tmp = cJSON_GetObjectItem(p_osd, "p_x");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("p_x error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int p_x = p_tmp->valueint;

			// p_y
			p_tmp = cJSON_GetObjectItem(p_osd, "p_y");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("p_y error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int p_y = p_tmp->valueint;

			// img_url：logo 最多可以保存3张
			cJSON * img_root = cJSON_GetObjectItem(p_osd, "img_url");
			if (!img_root) 
			{
				cgi_debug("img_url error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			cJSON *img_item = NULL;
			unsigned int img_num = cJSON_GetArraySize(img_root);
			if (img_num <= 0 || img_num > 3) 
			{
				cgi_debug("img_num error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}

			// period
			p_tmp = cJSON_GetObjectItem(p_osd, "period");
			if (!p_tmp) 
			{
				cgi_debug("no period error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}

			cJSON *pe_item = NULL;
			unsigned int pe_num = cJSON_GetArraySize(p_tmp);
			if (pe_num <= 0) 
			{
				cgi_debug("parse period array num error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}

			int ret = db.Exec("begin;");	// 开启事务
			if (SQLITE_OK != ret) 
			{
				cgi_debug("start transaction error");
				cJSON_Delete(root);
				return bwenc_common_ack(BWENC_ERROR_DB_ERR);
			}

			char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
			DateTime dt;
			dt.time2string(time(nullptr), timeStr);

			// 插入period
			cJSON* period_tmp = NULL;
			for (unsigned int i = 0; i < pe_num; i++) 
			{
				pe_item = cJSON_GetArrayItem(p_tmp, i);
				period_tmp = cJSON_GetObjectItem(pe_item, "day");
				if (!period_tmp || period_tmp->valueint < 0 || period_tmp->valueint > 6) 
				{
					cgi_debug("period day error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				int day = period_tmp->valueint;

				// start_time
				period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
				DateTime dt;
					
				if (!period_tmp || !period_tmp->valuestring || strlen(period_tmp->valuestring) != 8 || !dt.timeCheck(period_tmp->valuestring)) 
				{
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
						
				char start_time[10] = { '\0' };
				memcpy(start_time, period_tmp->valuestring,
						strlen(period_tmp->valuestring));

				// end_time
				period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
				if (!period_tmp || !period_tmp->valuestring || strlen(period_tmp->valuestring) != 8 || !dt.timeCheck(period_tmp->valuestring)) 
				{
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				char end_time[10] = { 0 };
				memcpy(end_time, period_tmp->valuestring, strlen(period_tmp->valuestring));

				// state
				int state = 0;
				period_tmp = cJSON_GetObjectItem(pe_item, "state");
				if (!period_tmp
						|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
					cgi_debug("period state error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				state = period_tmp->valueint;

				//先删除原来的
				snprintf(sql, sizeof(sql),
						"delete from %s where %s=%d and %s=%d",
						DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_OSD_ID,i);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("del old time  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				
				// 插入period数据
				snprintf(sql, sizeof(sql),
						"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
						DBTBL_OSD_PERIOD, 
						DBCOL_CHL_ID, DBCOL_OSD_ID, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
						chl_id, i, day, start_time, end_time,state);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("insert sb_set  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
			}
			db.Exec("commit;");//关闭事务

			//设置到共享内存；
			if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
			{
				chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
				osd_info = &(chl_info->encchl_osd[i]);

				//清空
				memset(osd_info,0,sizeof(stENC_OSDSET));
				
				osd_info->osd_enable = 1;
				osd_info->osd_type = BWENC_OSD_TYPE_LOGO;
				osd_info->priority = priority;

				logo_info = &(osd_info->osd_cfg.osd_logo);
				logo_info->logo_opacity = opacity;
				logo_info->logo_width = width;
				logo_info->logo_height = height;
				logo_info->logo_x = p_x;
				logo_info->logo_y = p_y;
				logo_info->logo_movspeed = slide_interval;
				logo_info->logo_num = img_num;

				for (unsigned int j = 0; j < img_num; j++) 
				{
					if( j >= ENC_OSD_MAX_SLIDE_NUM)
					{
						break;
					}
					
					img_item = cJSON_GetArrayItem(img_root, j);
					if (img_item) 
					{
						snprintf((char *)logo_info->logo_url[j], sizeof(logo_info->logo_url[j]), "%s",img_item->valuestring);
					}

				}

				//生效时间
				for(int d = 0; d < ENC_WEEK_DAYS; d++)
				{
					snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
												DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_OSD_ID,i,DBCOL_DAY, d,DBCOL_STATE);

					BWRows period_rows = db.QeuryRows(sql);
					if(period_rows.empty() || period_rows.size()==0)
					{
						continue;
					}
					
					for (unsigned int t = 0; t < period_rows.size(); t++) 
					{
						if(t >= ENC_OSD_MAX_TM_NUM)
						{
							break;
						}
						
						BWRow p_row = period_rows[t];

						(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
						(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
					}
				}				
			}
		}
		else if(osd_type == BWENC_OSD_TYPE_TEXT)
		{
			p_tmp = cJSON_GetObjectItem(p_osd, "priority");
			priority = p_tmp->valueint > 7 ? 7 : p_tmp->valueint;
			
			p_tmp = cJSON_GetObjectItem(p_osd, "roll_speed");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| (strcmp(p_tmp->valuestring, BWENC_RS_SLOW) != 0
							&& strcmp(p_tmp->valuestring, BWENC_RS_NORMAL) != 0
							&& strcmp(p_tmp->valuestring, BWENC_RS_QUICK) != 0)) {
				cgi_debug("roll_speed error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char roll_speed[10] = { 0 };
			memcpy(roll_speed, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// dis_w
			p_tmp = cJSON_GetObjectItem(p_osd, "dis_w");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("dis_w error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int dis_w = p_tmp->valueint;
		
			// dis_h
			p_tmp = cJSON_GetObjectItem(p_osd, "dis_h");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("dis_h error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int dis_h = p_tmp->valueint;
		
			// p_x
			p_tmp = cJSON_GetObjectItem(p_osd, "p_x");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("p_x error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int p_x = p_tmp->valueint;
		
			// p_y
			p_tmp = cJSON_GetObjectItem(p_osd, "p_y");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("p_y error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int p_y = p_tmp->valueint;
		
			// font
			p_tmp = cJSON_GetObjectItem(p_osd, "font");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| (strcmp(p_tmp->valuestring, BWENC_FONT_AILERON) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_IDEALIST) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_KELSON) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_BITTER) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_ZWODERI) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_BRAXTON) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_SIMHEI) != 0) )
			{
				cgi_debug("font error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char font[20] = { 0 };
			memcpy(font, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// font_size
			p_tmp = cJSON_GetObjectItem(p_osd, "font_size");
			if (!p_tmp) 
			{
				cgi_debug("font_size error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int font_size = p_tmp->valueint;
		
			// font_type
			p_tmp = cJSON_GetObjectItem(p_osd, "font_type");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| (strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_NORMAL) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_BOLD) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_ITALIC) != 0)) {
				cgi_debug("font_type error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char font_type[10] = { 0 };
			memcpy(font_type, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// font_color
			p_tmp = cJSON_GetObjectItem(p_osd, "font_color");
		
			if (!p_tmp || !p_tmp->valuestring)
			{
				cgi_debug("font_color error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			char font_color[20] = { 0 };
			memcpy(font_color, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// back_color
			p_tmp = cJSON_GetObjectItem(p_osd, "back_color");
		
			if (!p_tmp || !p_tmp->valuestring)
			{
				cgi_debug("back_color error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			char back_color[20] = { 0 };
			memcpy(back_color, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// content
			p_tmp = cJSON_GetObjectItem(p_osd, "content");
			if (!p_tmp || !p_tmp->valuestring
					|| strlen(p_tmp->valuestring)
							<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
				cgi_debug("content error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char content[BWENC_SUB_LEN_MAX] = { 0 };
			memcpy(content, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// period
			p_tmp = cJSON_GetObjectItem(p_osd, "period");
			if (!p_tmp) {
				cgi_debug("no period error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			cJSON *pe_item = NULL;
			unsigned int pe_num = cJSON_GetArraySize(p_tmp);
			if (pe_num <= 0) {
				cgi_debug("parse period array num error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			int ret = db.Exec("begin;");	// 开启事务
			if (SQLITE_OK != ret) {
				cgi_debug("start transaction error");
				cJSON_Delete(root);
				return bwenc_common_ack(BWENC_ERROR_DB_ERR);
			}
			char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
			DateTime dt;
			dt.time2string(time(nullptr), timeStr);
		
			cJSON* period_tmp = NULL;
			for (unsigned int i = 0; i < pe_num; i++) 
			{
				pe_item = cJSON_GetArrayItem(p_tmp, i);
				period_tmp = cJSON_GetObjectItem(pe_item, "day");
				if (!period_tmp || period_tmp->valueint < 0
						|| period_tmp->valueint > 6) {
					cgi_debug("period day error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				int day = period_tmp->valueint;
		
				// start_time
				period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
				DateTime dt;
				if (!period_tmp || !period_tmp->valuestring
						|| strlen(period_tmp->valuestring) != 8
						|| !dt.timeCheck(period_tmp->valuestring)) {
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				char start_time[10] = { 0 };
				memcpy(start_time, period_tmp->valuestring,
						strlen(period_tmp->valuestring));
		
				// end_time
				period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
				if (!period_tmp || !period_tmp->valuestring
						|| strlen(period_tmp->valuestring) != 8
						|| !dt.timeCheck(period_tmp->valuestring)) {
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				char end_time[10] = { 0 };
				memcpy(end_time, period_tmp->valuestring,
						strlen(period_tmp->valuestring));
		
				// state
				int state = 0;
				period_tmp = cJSON_GetObjectItem(pe_item, "state");
				if (!period_tmp
						|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
					cgi_debug("period state error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				state = period_tmp->valueint;
		
				//先删除原来的
				snprintf(sql, sizeof(sql),
						"delete from %s where %s=%d and %s=%d",
						DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_OSD_ID,i);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("insert sb_set  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				
				// 插入period数据
				snprintf(sql, sizeof(sql),
						"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
						DBTBL_OSD_PERIOD, 
						DBCOL_CHL_ID, DBCOL_OSD_ID, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
						chl_id, i, day, start_time, end_time,state);
				
				if (SQLITE_OK != db.Exec(sql)) {
					cgi_debug("insert sb_set  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
		
			}
		
			db.Exec("commit;");//关闭事务
			
		//设置到共享内存
			if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
			{
				chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
				osd_info = &(chl_info->encchl_osd[i]);
		
				//清空
				memset(osd_info,0,sizeof(stENC_OSDSET));
				
				osd_info->osd_enable = 1;
				osd_info->osd_type = BWENC_OSD_TYPE_TEXT;
				osd_info->priority = priority;
		
				sub_info = &(osd_info->osd_cfg.osd_sub);
				
				snprintf((char *)sub_info->sub_content, sizeof(sub_info->sub_content),"%s",content);
				//snprintf((char *)sub_info->sub_font, sizeof(sub_info->sub_font),"%s",font);
				snprintf((char *)sub_info->sub_font, sizeof(sub_info->sub_font),"%s.%s",font,"ttf");
				
				sub_info->sub_fontsize = font_size;
				
				if (0 == strcmp(font_type,BWENC_FONT_TYPE_NORMAL)) 
				{
					sub_info->sub_fontstyle = 0;
				} 
				else if (0 == strcmp(font_type,BWENC_FONT_TYPE_BOLD)) 
				{
					sub_info->sub_fontstyle = 1;
				} 
				else if (0 == strcmp(font_type,BWENC_FONT_TYPE_ITALIC)) 
				{
					sub_info->sub_fontstyle = 2;
				} 
		
				sub_info->sub_fontcolor = atoi(font_color);
				sub_info->sub_backcolor = atoi(back_color);
				
				sub_info->sub_x = p_x;
				sub_info->sub_y = p_y;
				sub_info->sub_width = dis_w;
				sub_info->sub_height = dis_h;
				
				if (0 == strcmp(roll_speed,BWENC_RS_SLOW)) 
				{
					sub_info->sub_movspeed = BWENC_RS_SLOW_T;
				} 
				else if (0 == strcmp(roll_speed,BWENC_RS_NORMAL)) 
				{
					sub_info->sub_movspeed = BWENC_RS_NORMAL_T;
				} 
				else if (0 == strcmp(roll_speed,BWENC_RS_QUICK)) 
				{
					sub_info->sub_movspeed = BWENC_RS_QUICK_T;
				}
		
				//生效时间
				for(int d = 0; d < ENC_WEEK_DAYS; d++)
				{
					snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
												DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_OSD_ID, i,DBCOL_DAY, d,DBCOL_STATE);
		
					BWRows period_rows = db.QeuryRows(sql);
					if(period_rows.empty() || period_rows.size()==0)
					{
						continue;
					}
					
					for (unsigned int t = 0; t < period_rows.size(); t++) 
					{
						if(t >= ENC_OSD_MAX_TM_NUM)
						{
							break;
						}
						
						BWRow p_row = period_rows[t];
		
						(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
						(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
					}
				}
			}
		}
		else if(osd_type == BWENC_OSD_TYPE_QRCODE)
		{	
			p_tmp = cJSON_GetObjectItem(p_osd, "priority");
			priority = p_tmp->valueint > 7 ? 7 : p_tmp->valueint;
			
			int opacity = 0;
			p_tmp = cJSON_GetObjectItem(p_osd, "opacity");
			if (p_tmp) {
				if (p_tmp->valueint < 0 || p_tmp->valueint > 10) {
					cgi_debug("opacity error");
					code = BWENC_ERROR_INVALID_PARAM;
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				opacity = p_tmp->valueint;
			}
		
			// width
			p_tmp = cJSON_GetObjectItem(p_osd, "width");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("width error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int width = p_tmp->valueint;
		
			// height
			p_tmp = cJSON_GetObjectItem(p_osd, "height");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("height error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int height = p_tmp->valueint;
		
			// dis_x
			p_tmp = cJSON_GetObjectItem(p_osd, "dis_x");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_W) {
				cgi_debug("dis_x error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int dis_x = p_tmp->valueint;
		
			// dis_y
			p_tmp = cJSON_GetObjectItem(p_osd, "dis_y");
			if (!p_tmp || p_tmp->valueint <= 0 || p_tmp->valueint > BWENC_PIC_MAX_H) {
				cgi_debug("dis_y error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int dis_y = p_tmp->valueint;
		
			// font
			p_tmp = cJSON_GetObjectItem(p_osd, "font");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| (strcmp(p_tmp->valuestring, BWENC_FONT_AILERON) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_IDEALIST) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_KELSON) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_BITTER) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_ZWODERI) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_BRAXTON) != 0)) {
				cgi_debug("font error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char font[20] = { 0 };
			memcpy(font, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// font_size
			p_tmp = cJSON_GetObjectItem(p_osd, "font_size");
			if (!p_tmp )
			{
				cgi_debug("font_size error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int font_size = p_tmp->valueint;
		
			// font_type
			p_tmp = cJSON_GetObjectItem(p_osd, "font_type");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| (strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_NORMAL) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_BOLD) != 0
							&& strcmp(p_tmp->valuestring, BWENC_FONT_TYPE_ITALIC) != 0)) {
				cgi_debug("font_type error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char font_type[20] = { 0 };
			memcpy(font_type, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// font_color
			p_tmp = cJSON_GetObjectItem(p_osd, "font_color");
			if (!p_tmp || !p_tmp->valuestring || strlen(p_tmp->valuestring) <= 0
					|| strlen(p_tmp->valuestring) > 7) {
				cgi_debug("font_color error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char font_color[20] = { 0 };
			memcpy(font_color, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			p_tmp = cJSON_GetObjectItem(p_osd, "text_p");
			if (!p_tmp
					|| (p_tmp->valueint != BWENC_TEXT_POS_UP
							&& p_tmp->valueint != BWENC_TEXT_POS_DOWN)) {
				cgi_debug("text_p error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			int text_p = p_tmp->valueint;
		
			//qrcode url
			p_tmp = cJSON_GetObjectItem(p_osd, "qr_url");
			if (!p_tmp || !p_tmp->valuestring
					|| strlen(p_tmp->valuestring)
							<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
				cgi_debug("url error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char qr_url[BWENC_SUB_LEN_MAX] = { '\0' };
			memcpy(qr_url, p_tmp->valuestring, strlen(p_tmp->valuestring));

			char img_url[BWENC_SUB_LEN_MAX] = { '\0' };//保留
		
			// text
			p_tmp = cJSON_GetObjectItem(p_osd, "text");
			if (!p_tmp || !p_tmp->valuestring
					|| strlen(p_tmp->valuestring)
							<= 0||strlen(p_tmp->valuestring)>BWENC_QR_TEXT_LEN_MAX) {
				cgi_debug("text error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char text[BWENC_QR_TEXT_LEN_MAX] = { '\0' };
			memcpy(text, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// text_url
			p_tmp = cJSON_GetObjectItem(p_osd, "text_url");
			if (!p_tmp || !p_tmp->valuestring
					|| strlen(p_tmp->valuestring)
							<= 0||strlen(p_tmp->valuestring)>BWENC_SUB_LEN_MAX) {
				cgi_debug("logo_url error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
			char text_url[BWENC_SUB_LEN_MAX] = { '\0' };
			memcpy(text_url, p_tmp->valuestring, strlen(p_tmp->valuestring));
		
			// period
			p_tmp = cJSON_GetObjectItem(p_osd, "period");
			if (!p_tmp) {
				cgi_debug("no period error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			cJSON *pe_item = NULL;
			unsigned int pe_num = cJSON_GetArraySize(p_tmp);
			if (pe_num <= 0) {
				cgi_debug("parse period array num error");
				code = BWENC_ERROR_INVALID_PARAM;
				cJSON_Delete(root);
				return bwenc_common_ack(code);
			}
		
			int ret = db.Exec("begin;");	// 开启事务
			if (SQLITE_OK != ret) {
				cgi_debug("start transaction error");
				cJSON_Delete(root);
				return bwenc_common_ack(BWENC_ERROR_DB_ERR);
			}
		
			DateTime dt;
			char timeStr[sizeof("yyyy-mm-dd hh:mm:ss")] = { '\0' };
			dt.time2string(time(nullptr), timeStr);
			
			cJSON* period_tmp = NULL;
			for (unsigned int i = 0; i < pe_num; i++) {
				pe_item = cJSON_GetArrayItem(p_tmp, i);
				period_tmp = cJSON_GetObjectItem(pe_item, "day");
				if (!period_tmp || period_tmp->valueint < 0
						|| period_tmp->valueint > 6) {
					cgi_debug("period day error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				int day = period_tmp->valueint;
		
				// start_time
				period_tmp = cJSON_GetObjectItem(pe_item, "start_time");
				DateTime dt;
				if (!period_tmp || !period_tmp->valuestring
						|| strlen(period_tmp->valuestring) != 8
						|| !dt.timeCheck(period_tmp->valuestring)) {
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				char start_time[10] = { 0 };
				memcpy(start_time, period_tmp->valuestring,
						strlen(period_tmp->valuestring));
		
				// end_time
				period_tmp = cJSON_GetObjectItem(pe_item, "end_time");
				if (!period_tmp || !period_tmp->valuestring
						|| strlen(period_tmp->valuestring) != 8
						|| !dt.timeCheck(period_tmp->valuestring)) {
					cgi_debug("period start_time error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				char end_time[10] = { 0 };
				memcpy(end_time, period_tmp->valuestring,
						strlen(period_tmp->valuestring));
		
				// state
				int state = 0;
				period_tmp = cJSON_GetObjectItem(pe_item, "state");
				if (!period_tmp
						|| (period_tmp->valueint != 0 && period_tmp->valueint != 1)) {
					cgi_debug("period state error");
					code = BWENC_ERROR_INVALID_PARAM;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				state = period_tmp->valueint;

				//先删除原来的qr记录
				snprintf(sql, sizeof(sql),
						"delete from %s where %s=%d and %s=%d",
						DBTBL_QRCODE_SET,  DBCOL_CHL_ID, chl_id, DBCOL_OSD_ID,i);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("del qr  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				// 插入qrcode_set
				snprintf(sql, sizeof(sql),
						"INSERT INTO %s (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s) VALUES (%d,%d,'%s',%d,'%s','%s',%d,'%s','%s','%s','%s')",
						DBTBL_QRCODE_SET, 
						DBCOL_CHL_ID, DBCOL_OSD_ID, 
						DBCOL_FONT,DBCOL_FONT_SIZE, DBCOL_FONT_TYPE, DBCOL_FONT_COLOR,
						DBCOL_TEXT_P, DBCOL_TEXT, DBCOL_TEXT_URL, DBCOL_IMG_URL, DBCOL_CREATE_TIME,
						chl_id, i, 
						font, font_size, font_type, font_color, text_p, text, text_url, img_url, timeStr);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("insert qr  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				
		
				//先删除原来的 时间
				snprintf(sql, sizeof(sql),
						"delete from %s where %s=%d and %s=%d",
						DBTBL_OSD_PERIOD,  DBCOL_CHL_ID, chl_id, DBCOL_OSD_ID,i);
				if (SQLITE_OK != db.Exec(sql)) 
				{
					cgi_debug("del time  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
				
				// 插入period数据
				snprintf(sql, sizeof(sql),
						"insert into %s (%s,%s,%s,%s,%s,%s) VALUES (%d,%d,%d,'%s','%s',%d)",
						DBTBL_OSD_PERIOD, 
						DBCOL_CHL_ID, DBCOL_OSD_ID, DBCOL_DAY, DBCOL_START_TIME,DBCOL_END_TIME, DBCOL_STATE, 
						chl_id, i, day, start_time, end_time,state);
				
				if (SQLITE_OK != db.Exec(sql)) {
					cgi_debug("insert sb_set  error");
					code = BWENC_ERROR_DB_ERR;
					db.Exec("rollback;");
					cJSON_Delete(root);
					return bwenc_common_ack(code);
				}
		
			}
			
			db.Exec("commit;");
		
		//设置到共享内存 			
			if(chl_id > 0 && chl_id <= ENC_TOTAL_CHLMAX)
			{
				chl_info = &(g_encset_mst->enc_chls[chl_id - 1]);
				osd_info = &(chl_info->encchl_osd[i]);
		
				//清空
				memset(osd_info,0,sizeof(stENC_OSDSET));
				
				osd_info->osd_enable = 1;
				osd_info->osd_type = BWENC_OSD_TYPE_QRCODE;
			    osd_info->priority = priority;
		
				qr_info = &(osd_info->osd_cfg.osd_qr);				
				snprintf((char *)qr_info->qr_url, sizeof(qr_info->qr_url),"%s",qr_url);
				qr_info->qr_opacity = opacity;
				qr_info->qr_width = width;
				qr_info->qr_height = height;
				qr_info->qr_x = dis_x;
				qr_info->qr_y = dis_y;
				
		
				//生效时间
				for(int d = 0; d < ENC_WEEK_DAYS; d++)
				{
					snprintf(sql, sizeof(sql),"SELECT * FROM %s WHERE %s=%d and %s=%d and %s=%d and %s=1 order by start_time asc",
												DBTBL_OSD_PERIOD, DBCOL_CHL_ID,chl_id,DBCOL_OSD_ID,i,DBCOL_DAY, d,DBCOL_STATE);
		
					BWRows period_rows = db.QeuryRows(sql);
					if(period_rows.empty() || period_rows.size()==0)
					{
						continue;
					}
					
					for (unsigned int t = 0; t < period_rows.size(); t++) 
					{
						if(t >= ENC_OSD_MAX_TM_NUM)
						{
							break;
						}
						
						BWRow p_row = period_rows[t];
		
						(osd_info->osd_effect_tm)[d][t].tm_start_sec = dt.time2sec(p_row[DBCOL_START_TIME]);
						(osd_info->osd_effect_tm)[d][t].tm_end_sec = dt.time2sec(p_row[DBCOL_END_TIME]);
					}
				}
			}
		}
	}

	chl_info->encchl_setvsion++;
	
	snprintf(logstr,sizeof(logstr),"slot:%d-ch:%d osd set successfully",s_id,c_id);
	log_write(BWENC_LOG_SYS_SET, logstr);

	cJSON_Delete(root);
	return bwenc_common_ack(code);
}

